package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.login.AuthLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.AlbumListVo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.query.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    /**
     * 接口需要用户登录权限
     *
     * @保存声音
     */
    @Operation(summary = "保存声音")
    @PostMapping("/trackInfo/saveTrackInfo")
    @AuthLogin
    public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.saveTrackInfo(trackInfoVo);
        return Result.ok();
    }

    /**
     * 接口需用户登录权限
     *
     * @获取当前用户声音分页列表
     */
    @Operation(summary = "获取当前用户声音分页列表")
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    @AuthLogin
    public Result<IPage<TrackListVo>> findUserTrackPage(
            @PathVariable Long page,
            @PathVariable Long limit,
            @RequestBody TrackInfoQuery trackInfoQuery) {
        IPage<TrackListVo> albumListVoIPage = new Page<>(page, limit);
        albumListVoIPage = trackInfoService.findUserTrackPage(albumListVoIPage, trackInfoQuery);
        return Result.ok(albumListVoIPage);
    }

    /**
     * 回显声音详情
     *
     * @param id 声音ID
     * @return TrackInfo 声音详情信息
     */
    @Operation(summary = "回显声音详情")
    @GetMapping("/trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        TrackInfo trackInfo = trackInfoService.getById(id);
        return Result.ok(trackInfo);
    }

    /**
     * 修改声音信息
     *
     * @param id        声音ID
     * @param trackInfo 声音修改信息
     */
    @Operation(summary = "修改声音信息")
    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable Long id, @Validated @RequestBody TrackInfoVo trackInfoVo) {
        trackInfoService.updateTrackInfo(id, trackInfoVo);
        return Result.ok();
    }

    /**
     * 删除声音信息
     *
     * @param id 声音ID
     */
    @Operation(summary = "删除声音信息")
    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }

    /**
     * 分页获取专辑声音详情
     *
     * @param albumId 专辑id
     * @param page    分页参数
     * @param limit   分页参数
     * @return albumTrackListVoPage
     */
    @Operation(summary = "查询专辑声音分页列表")
    @AuthLogin(required = false)
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId, @PathVariable Integer page, @PathVariable Integer limit) {
        Page<AlbumTrackListVo> albumTrackListVoPage = new Page<>(page, limit);
        albumTrackListVoPage = trackInfoService.findAlbumTrackPage(albumTrackListVoPage, albumId);
        return Result.ok(albumTrackListVoPage);
    }

    /**
     * 获取声音统计信息
     *
     * @param trackId 音频ID
     */
    @Operation(summary = "获取声音统计信息")
    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
        TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
        return Result.ok(trackStatVo);
    }

    @AuthLogin
    @Operation(summary = "基于用户选择声音作为起始标准获取未购买声音数量，得到分集购买列表")
    @GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId) {
        Long userId = AuthContextHolder.getUserId();
        List<Map<String, Object>> list = trackInfoService.findUserTrackPaidList(userId, trackId);
        return Result.ok(list);
    }

    @AuthLogin
    @Operation(summary = "获取用户专辑未购买声音列表")
    @GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> findWaitBuyTrackList(@PathVariable Long trackId, @PathVariable Integer trackCount) {
        Long userId = AuthContextHolder.getUserId();
        List<TrackInfo> list = trackInfoService.findPaidTrackInfoList(userId, trackId, trackCount);
        return Result.ok(list);
    }
}

